﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;

public partial class User_dang_ky : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtPassword.Attributes.Add("onKeyPress",
                           "doClick('" + btnDangnhap.ClientID + "',event)");
    }
    protected void btnDangnhap_Click(object sender, EventArgs e)
    {
        //try
        //{
            string password1 = Md5(txtPassword.Text);
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_CheckLogin ";
            Dal.ParameterCollection = new string[2] { "@UserEmail", "@MatKhau" };
            Dal.ValueCollection = new string[2] { txtEmail.Text, password1 };
            Dal.ReturnValueParameter = "@ReturnValue";
            Dal.ExecuteNonQuery();
            if (Dal.ReturnValue == 0)
            {
                lblInfo.Text = "Email không tồn tại";
                clear();
            }
            if (Dal.ReturnValue == 2)
            {
                lblInfo.Text = "Sai mật khẩu";
                txtPassword.Text = "";
                txtPassword.Focus();
            }
            if (Dal.ReturnValue == 1)
            {
                Session.Add("UserEmail", txtEmail.Text);
                Session.Add("TenHienThi",GetDisPlayName());
                if (cbRemember.Checked)
                {
                    HttpCookie useremail = new HttpCookie("UserEmail");
                    HttpCookie password = new HttpCookie("PassWord");
                    Response.Cookies["UserEmail"].Value = txtEmail.Text;
                    Response.Cookies["PassWord"].Value = txtPassword.Text;
                    Response.Cookies["UserEmail"].Expires = DateTime.Now.AddMonths(2);
                    Response.Cookies["PassWord"].Expires = DateTime.Now.AddMonths(2);
                    Response.Cookies.Add(useremail);
                    Response.Cookies.Add(password);
                }
                else
                {
                    HttpCookie useremail = new HttpCookie("UserEmail");
                    HttpCookie password = new HttpCookie("PassWord");
                    Response.Cookies["UserEmail"].Value = txtEmail.Text;
                    Response.Cookies["PassWord"].Value = txtPassword.Text;
                    Response.Cookies["UserEmail"].Expires = DateTime.Now.AddMonths(-1);
                    Response.Cookies["PassWord"].Expires = DateTime.Now.AddMonths(-1);
                    Response.Cookies.Add(useremail);
                    Response.Cookies.Add(password);
                }
                Response.Redirect("~/Default.aspx");
            }
            if (Dal.ReturnValue == 3)
                lblInfo.Text = "Bạn chưa kích hoạt tài khoản, vui lòng kiểm tra Email để kích hoạt.";

        //}
        //catch
        //{
        //    lblInfo.Text = "Có lỗi hệ thống xảy ra, vui lòng đăng nhập lại";
        //}
        
        
    }
    protected void clear()
    {
        txtEmail.Text = "";
        txtPassword.Text = "";
    }
    protected string GetDisPlayName()
    {
        string displayname = "";
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.Text;
            Dal.CommandText = "select TenHienThi from NguoiDung where USERS.UserEmail=@UserEmail";
            Dal.ParameterCollection = new string[1] { "@UserEmail" };
            Dal.ValueCollection = new string[1] { txtEmail.Text };
            DataRow dr = Dal.GetDataRow();
            displayname = dr[0].ToString();
        }
        catch
        {
            lblInfo.Text = "Lỗi hệ thống, vui lòng đăng nhập lại";
        }
        return displayname;

    }
    public static string Md5(string strPass)
    {
        try
        {
            MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider();
            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(strPass));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
        catch (Exception objEx)
        {
        }
        return string.Empty;
    }
    
}
